/* File:          assign1.cpp
   Title:         Motorcycle Inventory
   Submitted by:  Stephen Durfey
   For Course:    CS 202
   Due:           January 28, 2008

   Purpose:

   This program is designed to manage the inventory for a motorcycle shop. It will read in all necessary data, including 
   command types, from a data file. The data will either be added or deleted from the inventory. Also, the price can 
   be updated and the inventory can be sorted in in descending order, or just printed in the order the data was 
   entered into the inventory listing.

   Functions:
   
   1) This function uses vectors to add a motorcycle object into the inventory by utilizing the push_back() function. 
      All necessary data is read in through the data file and then the class constructor is called to instatiate the 
	  object and push it onto the the vector.

   2) This function will allow the deletion of a vector and keep the order in which the data was entered in place. 
      All necessary data is read in through the provided data file. If the stock number isn't found, the user
	  will be informed.

   3) This function allows for the price of any bike to be updated. The necessary data will be read in via the 
      data file, the values of the price will be changed adn then printed in a format that is easily accessible. 
	  The old price will be printed as well as the new price of the make and model. If the data is not found
	  then the user will be informed, but the data file will continue to be read.

   4) This function will provide a listing of all models of the make read in via the data file. 

   5) This function utilizes the bubble sort algorithm to order the prices of the bikes in the current inventory 
      in descending order. This will be the only place in the program where the sorted vector can be accessed. 
	  It will remain unchanged in every other part of the program

   6) This function will merely print out the the current inventory stock in an easily readable format.

   This program will continue until a '7' is read in, this will end the program. */
   

#include "motorcycle.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>


using namespace std;

void addMotorcycle(vector<Motorcycle> &motoVect);
void removeMoto(vector<Motorcycle> &motoVect, int Stock);
void changePrice(vector<Motorcycle> &motoVect, int Stock, float Price);
void printMake(vector<Motorcycle> &motoVect, string motoType);
void sortVect(vector<Motorcycle> motoVect);
void printVect(vector<Motorcycle> &motoVect);


ifstream inData;

int main()
{
	vector<Motorcycle> motorcycle;
	int commandCode, stockNum;
	float price;
	string Make;

	cout << fixed << setprecision(2);

	inData.open("assign1bikes.dat");

	if(!inData)
	{
		cout << "File failed to open!\n\n";
		return 0;
	}

	inData >> commandCode;
	inData.ignore();

	while( !inData.eof() )
	{
		if(commandCode == 1)
		{
			addMotorcycle(motorcycle);
		}

		else if(commandCode == 2)
		{
			inData >> stockNum;

			removeMoto(motorcycle, stockNum);
		}

		else if(commandCode == 3)
		{
			changePrice(motorcycle, stockNum, price);
		}
		
		else if(commandCode ==4)
		{
			printMake(motorcycle, Make);
		}

		else if(commandCode == 5)
		{
			cout << "Sorted Inventory by Price:\n\n";
			cout << left << setw(15) << "Make" << setw(18) << "Model" << setw(16) << "StockNum" << setw(14) << "Style" << "Price" << endl;
			cout << setw(15) << "-----" << setw(18) << "-----" << setw(16) << "--------" << setw(14) << "-----"  << "-----" << endl << endl;

			sortVect(motorcycle);
		}

		else if(commandCode == 6)
		{
			cout << "Current Inventory:\n\n";
			cout << left << setw(15) << "Make" << setw(18) << "Model" << setw(16) << "StockNum" << setw(14) << "Style" << "Price" << endl;
			cout << setw(15) << "-----" << setw(18) << "-----" << setw(16) << "--------" << setw(14) << "-----"  << "-----" << endl << endl;

			printVect(motorcycle);
		}
		else if(commandCode == 7)
			return 0;

		inData >> commandCode;
		inData.ignore();
	}

	return 0;
}

//*function 1, see documentation at top*

void addMotorcycle(vector<Motorcycle> &motoVect)
{
	string fmake, fmodel, fstyle;
	int fstocknum;
	float fprice;

	getline(inData, fmake);
	getline(inData, fmodel);
	inData >> fstocknum;
	inData.ignore();
	getline(inData, fstyle);
	inData >> fprice;
	inData.ignore();

	motoVect.push_back(Motorcycle(fmake, fmodel, fstyle, fstocknum, fprice));

	cout << "Your motorcycle has been added to the inventory: " << fstocknum << endl << endl;
}

//*function 2, see documentation at top*

void removeMoto(vector<Motorcycle> &motoVect, int Stock)
{
	bool find = false;

	for(int n = 0; n < motoVect.size(); n++)
	{
		if(motoVect[n].getStockNum() == Stock)
		{
			while(n != (motoVect.size() - 1))
			{
				motoVect[n] = motoVect[n + 1];
				n++;
			}

			motoVect.pop_back();
			find = true;
		}
	}

	if(find == false)
		cout << "Motorcycle is not in inventory!";
	else if(find == true)
		cout << "Your motorcycle " << Stock << " has been removed from inventory\n\n";
}

//*function 3, see documentation at top*

void changePrice(vector<Motorcycle> &motoVect, int Stock, float Price)
{
	bool find = false; //this boolean variable will 
	int k;

	inData >> Stock;
	inData >> Price;


	for(k = 0; k < motoVect.size(); k++)
	{
		if(motoVect[k].getStockNum() == Stock)
		{
			cout << "Your bike price has been updated: \n";
			cout << "For " << motoVect[k].getMake() << ": " << motoVect[k].getModel() << endl;
			cout << "Old Price: $" << motoVect[k].getPrice() << endl;
			motoVect[k].setPrice(Price);
			cout << "New Price: $" << motoVect[k].getPrice() << endl << endl << endl << endl;
			find = true;
		}
	}


	if(find != true)
		cout << "Stock Number " << Stock << " was not found in inventory\n\n\n";
	
}

//*function 4, see documentation at top*

void printMake(vector<Motorcycle> &motoVect, string motoType)
{
	inData >> motoType;

	cout << "\n\nA list for all models of " << motoType << endl << endl;
	cout << left << setw(15) << "Make" << setw(18) << "Model" << setw(16) << "StockNum" << setw(14) << "Style" << "Price" << endl;
	cout << setw(15) << "-----" << setw(18) << "-----" << setw(16) << "--------" << setw(14) << "-----"  << "-----" << endl << endl;

	for(int n = 0; n < motoVect.size(); n++)
	{
		if(motoVect[n].getMake() == motoType)
		{
			motoVect[n].print();
		}
	}

	cout << endl << endl << endl;
}

//*function 5, see documentation at top*

void sortVect(vector<Motorcycle> motoVect)
{
	vector<Motorcycle> holder(1);

	bool swap;
	int elems = motoVect.size();

	do
	{	swap = false;
		for (int count = 0; count < (elems - 1); count++)
		{
			if (motoVect[count].getPrice() > motoVect[count + 1].getPrice())
			{
				holder[0] = motoVect[count];
				motoVect[count] = motoVect[count + 1];
				motoVect[count + 1] = holder[0];
				swap = true;
			}
		}
		elems--;
   } while (swap);

	for(int k = 0; k < motoVect.size(); k++)
	{
		motoVect[k].print();
	}

	cout << endl << endl << endl;
}

//*function 6, see documentation at top*

void printVect(vector<Motorcycle> &motoVect)
{
	for(int n = 0; n < motoVect.size(); n++)
	{
		motoVect[n].print();
	}

	cout << endl << endl << endl;
}



		





